package ar.unlam.com.tateti.blackboard;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ar.unlam.com.tateti.control.Controller;

public class Tablero extends JInternalFrame {

	private Controller controlador = null;
	private static final long serialVersionUID = 1L;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenu = null;
	private JMenuItem jMenuItem = null;
	private JMenuItem jMenuItem1 = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JPanel jPanel2 = null;
	private JPanel jPanel3 = null;
	private JPanel jPanel4 = null;
	private JPanel jPanel5 = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JButton jButton4 = null;
	private JButton jButton5 = null;
	private JButton jButton6 = null;
	private JButton jButton7 = null;
	private JButton jButton8 = null;
	private JPanel jPanel6 = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	public boolean fin;
	
	public Tablero(Controller controlador) {
		super();
		initialize(controlador);
	}

	private void initialize(Controller controlador) {
		this.setSize(352, 422);
		this.setLocation(new java.awt.Point(200, 200));
		this.setResizable(false);
		this.setContentPane(getJPanel());
		this.setJMenuBar(getJJMenuBar());
		this.setTitle("Ta Te Ti  ");
		this.controlador = controlador;
		controlador.inicializar(this);
	}

	public void limpiarTablero() {
		jButton.setIcon(null);
		jButton1.setIcon(null);
		jButton2.setIcon(null);
		jButton3.setIcon(null);
		jButton4.setIcon(null);
		jButton5.setIcon(null);
		jButton6.setIcon(null);
		jButton7.setIcon(null);
		jButton8.setIcon(null);
		jLabel2.setVisible(false);
		jLabel1.setIcon(new ImageIcon("source/cruz_chica.JPG"));
	}

	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			try {
				jJMenuBar = new JMenuBar();
				jJMenuBar.setPreferredSize(new java.awt.Dimension(20, 30));
				jJMenuBar.add(getJMenu());
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jJMenuBar;
	}

	private JMenu getJMenu() {
		if (jMenu == null) {
			try {
				jMenu = new JMenu();
				jMenu.setText("Archivo");
				jMenu.add(getJMenuItem());
				jMenu.add(getJMenuItem1());
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jMenu;
	}

	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			try {
				jMenuItem = new JMenuItem();
				jMenuItem.setText("Nuevo juego");
				jMenuItem.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						revancha();
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jMenuItem;
	}

	private JMenuItem getJMenuItem1() {
		if (jMenuItem1 == null) {
			try {
				jMenuItem1 = new JMenuItem();
				jMenuItem1.setText("Salir");
				jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						abandonar();
					}
				});

			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jMenuItem1;
	}

	private JPanel getJPanel() {
		if (jPanel == null) {
			try {
				jPanel = new JPanel();
				jPanel.setLayout(new BorderLayout());
				jPanel.add(getJPanel1(), java.awt.BorderLayout.NORTH);
				jPanel.add(getJPanel6(), java.awt.BorderLayout.SOUTH);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel;
	}

	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			try {
				jPanel1 = new JPanel();
				jPanel1.setPreferredSize(new java.awt.Dimension(0, 280));
				jPanel1.setLayout(new BorderLayout());
				jPanel1.add(getJPanel2(), java.awt.BorderLayout.NORTH);
				jPanel1.add(getJPanel3(), java.awt.BorderLayout.CENTER);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel1;
	}

	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			try {
				GridLayout gridLayout = new GridLayout();
				gridLayout.setRows(1);
				jPanel2 = new JPanel();
				jPanel2.setLayout(gridLayout);
				jPanel2.setPreferredSize(new java.awt.Dimension(10, 93));
				jPanel2.add(getJButton(), null);
				jPanel2.add(getJButton1(), null);
				jPanel2.add(getJButton2(), null);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel2;
	}

	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			try {
				jPanel3 = new JPanel();
				jPanel3.setLayout(new BorderLayout());
				jPanel3.add(getJPanel4(), java.awt.BorderLayout.NORTH);
				jPanel3.add(getJPanel5(), java.awt.BorderLayout.CENTER);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel3;
	}

	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			try {
				GridLayout gridLayout1 = new GridLayout();
				gridLayout1.setRows(1);
				jPanel4 = new JPanel();
				jPanel4.setLayout(gridLayout1);
				jPanel4.setPreferredSize(new java.awt.Dimension(10, 93));
				jPanel4.add(getJButton3(), null);
				jPanel4.add(getJButton4(), null);
				jPanel4.add(getJButton5(), null);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel4;
	}

	private JPanel getJPanel5() {
		if (jPanel5 == null) {
			try {
				GridLayout gridLayout2 = new GridLayout();
				gridLayout2.setRows(1);
				jPanel5 = new JPanel();
				jPanel5.setLayout(gridLayout2);
				jPanel5.add(getJButton6(), null);
				jPanel5.add(getJButton7(), null);
				jPanel5.add(getJButton8(), null);
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this,
						"Ups, No se pudo crear parte de la interfaz\n" + e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		return jPanel5;
	}

	private JButton getJButton() {
		if (jButton == null) {
			try {
				jButton = new JButton();
				jButton.setBackground(java.awt.Color.white);
				jButton.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(0);
					}
				});
			} catch (Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			try {
				jButton1 = new JButton();
				jButton1.setBackground(java.awt.Color.white);
				jButton1.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(1);
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton1;
	}

	private JButton getJButton2() {
		if (jButton2 == null) {
			try {
				jButton2 = new JButton();
				jButton2.setBackground(java.awt.Color.white);
				jButton2.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(2);
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton2;
	}

	private JButton getJButton3() {
		if (jButton3 == null) {
			try {
				jButton3 = new JButton();
				jButton3.setBackground(java.awt.Color.white);
				jButton3.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(3);
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton3;
	}

	private JButton getJButton4() {
		if (jButton4 == null) {
			try {
				jButton4 = new JButton();
				jButton4.setBackground(java.awt.Color.white);
				jButton4.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(4);
					}
				});

			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton4;
	}

	private JButton getJButton5() {
		if (jButton5 == null) {
			try {
				jButton5 = new JButton();
				jButton5.setBackground(java.awt.Color.white);
				jButton5.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(5);
					}
				});

			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton5;
	}

	private JButton getJButton6() {
		if (jButton6 == null) {
			try {
				jButton6 = new JButton();
				jButton6.setBackground(java.awt.Color.white);
				jButton6.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(6);
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton6;
	}

	private JButton getJButton7() {
		if (jButton7 == null) {
			try {
				jButton7 = new JButton();
				jButton7.setBackground(java.awt.Color.white);
				jButton7.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(7);
					}
				});
			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton7;
	}

	private JButton getJButton8() {
		if (jButton8 == null) {
			try {
				jButton8 = new JButton();

				jButton8.setBackground(java.awt.Color.white);
				jButton8.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						marcar(8);
					}
				});

			} catch (java.lang.Throwable e) {
				JOptionPane.showMessageDialog(this, "Ups, No se pudo crear el boton\n" + e.getMessage(),
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		return jButton8;
	}

	private JPanel getJPanel6() {
		if (jPanel6 == null) {
			try {
				jLabel2 = new JLabel();
				jLabel2.setBounds(new java.awt.Rectangle(49, 47, 275, 38));
				jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 5));
				jLabel2.setFont(new java.awt.Font("Rockwell Extra Bold", java.awt.Font.BOLD, 29));
				// jLabel2.setText("  GAME OVER ");
				jLabel2.setForeground(java.awt.Color.blue);
				jLabel2.setVisible(false);
				jLabel1 = new JLabel();
				jLabel1.setBounds(new java.awt.Rectangle(121, 7, 39, 36));
				jLabel1.setIcon(new ImageIcon("/cruz_chica.JPG"));
				jLabel1.setText("");
				jLabel = new JLabel();
				jLabel.setText("  Juega -->");
				jLabel.setFont(new java.awt.Font("Arial Black", java.awt.Font.BOLD, 18));
				jLabel.setBounds(new java.awt.Rectangle(4, 7, 117, 35));
				jPanel6 = new JPanel();
				jPanel6.setLayout(null);
				jPanel6.setPreferredSize(new java.awt.Dimension(10, 90));
				jPanel6.setMaximumSize(new java.awt.Dimension(10, 90));
				jPanel6.add(jLabel, null);
				jPanel6.add(jLabel1, null);
				jPanel6.add(jLabel2, null);
			} catch (java.lang.Throwable e) {
				// TODO: Something
			}
		}
		return jPanel6;
	}

	public void setearTurno(int id) {
		if ((id % 2) == 0) {
			jLabel1.setIcon(new ImageIcon("/cruz_chica.JPG"));
		} else {
			jLabel1.setIcon(new ImageIcon("/circulo_chico.JPG"));
		}

	}

	public void setearTablero(int i, int j, int id) {
		if (i == 0 && j == 0) {
			if ((id % 2) != 0)
				jButton.setIcon(new ImageIcon("source/cruz.JPG"));
			else
				jButton.setIcon(new ImageIcon("source/circulo.JPG"));
		} else {
			if (i == 0 && j == 1) {
				if ((id % 2) != 0)
					jButton1.setIcon(new ImageIcon("source/cruz.JPG"));
				else
					jButton1.setIcon(new ImageIcon("source/circulo.JPG"));
			} else {
				if (i == 0 && j == 2) {
					if ((id % 2) != 0)
						jButton2.setIcon(new ImageIcon("source/cruz.JPG"));
					else
						jButton2.setIcon(new ImageIcon("source/circulo.JPG"));
				} else {
					if (i == 1 && j == 0) {
						if ((id % 2) != 0)
							jButton3.setIcon(new ImageIcon("source/cruz.JPG"));
						else
							jButton3.setIcon(new ImageIcon("source/circulo.JPG"));
					} else {
						if (i == 1 && j == 1) {
							if ((id % 2) != 0)
								jButton4.setIcon(new ImageIcon("source/cruz.JPG"));
							else
								jButton4.setIcon(new ImageIcon("source/circulo.JPG"));
						} else {
							if (i == 1 && j == 2) {
								if ((id % 2) != 0)
									jButton5.setIcon(new ImageIcon("source/cruz.JPG"));
								else
									jButton5.setIcon(new ImageIcon("source/circulo.JPG"));
							} else {
								if (i == 2 && j == 0) {
									if ((id % 2) != 0)
										jButton6.setIcon(new ImageIcon("source/cruz.JPG"));
									else
										jButton6.setIcon(new ImageIcon("source/circulo.JPG"));
								} else {
									if (i == 2 && j == 1) {
										if ((id % 2) != 0)
											jButton7.setIcon(new ImageIcon("source/cruz.JPG"));
										else
											jButton7.setIcon(new ImageIcon("source/circulo.JPG"));
									} else {
										if (i == 2 && j == 2) {
											if ((id % 2) != 0)
												jButton8.setIcon(new ImageIcon("source/cruz.JPG"));
											else
												jButton8.setIcon(new ImageIcon("source/circulo.JPG"));
										}

									}
								}
							}

						}
					}

				}
			}
		}
	}

	public Controller getControlador() {
		return controlador;
	}

	public void setControlador(Controller controlador) {
		this.controlador = controlador;
	}

	public JMenuBar getjJMenuBar() {
		return jJMenuBar;
	}

	public void setjJMenuBar(JMenuBar jJMenuBar) {
		this.jJMenuBar = jJMenuBar;
	}

	public JMenu getjMenu() {
		return jMenu;
	}

	public void setjMenu(JMenu jMenu) {
		this.jMenu = jMenu;
	}

	public JMenuItem getjMenuItem() {
		return jMenuItem;
	}

	public void setjMenuItem(JMenuItem jMenuItem) {
		this.jMenuItem = jMenuItem;
	}

	public JMenuItem getjMenuItem1() {
		return jMenuItem1;
	}

	public void setjMenuItem1(JMenuItem jMenuItem1) {
		this.jMenuItem1 = jMenuItem1;
	}

	public JPanel getjPanel() {
		return jPanel;
	}

	public void setjPanel(JPanel jPanel) {
		this.jPanel = jPanel;
	}

	public JPanel getjPanel1() {
		return jPanel1;
	}

	public void setjPanel1(JPanel jPanel1) {
		this.jPanel1 = jPanel1;
	}

	public JPanel getjPanel2() {
		return jPanel2;
	}

	public void setjPanel2(JPanel jPanel2) {
		this.jPanel2 = jPanel2;
	}

	public JPanel getjPanel3() {
		return jPanel3;
	}

	public void setjPanel3(JPanel jPanel3) {
		this.jPanel3 = jPanel3;
	}

	public JPanel getjPanel4() {
		return jPanel4;
	}

	public void setjPanel4(JPanel jPanel4) {
		this.jPanel4 = jPanel4;
	}

	public JPanel getjPanel5() {
		return jPanel5;
	}

	public void setjPanel5(JPanel jPanel5) {
		this.jPanel5 = jPanel5;
	}

	public JButton getjButton() {
		return jButton;
	}

	public void setjButton(JButton jButton) {
		this.jButton = jButton;
	}

	public JButton getjButton1() {
		return jButton1;
	}

	public void setjButton1(JButton jButton1) {
		this.jButton1 = jButton1;
	}

	public JButton getjButton2() {
		return jButton2;
	}

	public void setjButton2(JButton jButton2) {
		this.jButton2 = jButton2;
	}

	public JButton getjButton3() {
		return jButton3;
	}

	public void setjButton3(JButton jButton3) {
		this.jButton3 = jButton3;
	}

	public JButton getjButton4() {
		return jButton4;
	}

	public void setjButton4(JButton jButton4) {
		this.jButton4 = jButton4;
	}

	public JButton getjButton5() {
		return jButton5;
	}

	public void setjButton5(JButton jButton5) {
		this.jButton5 = jButton5;
	}

	public JButton getjButton6() {
		return jButton6;
	}

	public void setjButton6(JButton jButton6) {
		this.jButton6 = jButton6;
	}

	public JButton getjButton7() {
		return jButton7;
	}

	public void setjButton7(JButton jButton7) {
		this.jButton7 = jButton7;
	}

	public JButton getjButton8() {
		return jButton8;
	}

	public void setjButton8(JButton jButton8) {
		this.jButton8 = jButton8;
	}

	public JPanel getjPanel6() {
		return jPanel6;
	}

	public void setjPanel6(JPanel jPanel6) {
		this.jPanel6 = jPanel6;
	}

	public JLabel getjLabel() {
		return jLabel;
	}

	public void setjLabel(JLabel jLabel) {
		this.jLabel = jLabel;
	}

	public JLabel getjLabel1() {
		return jLabel1;
	}

	public void setjLabel1(JLabel jLabel1) {
		this.jLabel1 = jLabel1;
	}

	public JLabel getjLabel2() {
		return jLabel2;
	}

	public void setjLabel2(JLabel jLabel2) {
		this.jLabel2 = jLabel2;
	}

	public boolean isFin() {
		return fin;
	}

	public void setFin(boolean fin) {
		this.fin = fin;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public boolean getFin() {
		return fin;
	}

	private void marcar(int posicion) {
		try {
			controlador.marcar(posicion);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Ups, No se pudo marcar en la posicion:\n" + e.getMessage(),
					"Error al marcar", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}

	private void revancha() {
		try {
			controlador.revancha();
			JOptionPane.showMessageDialog(null,
					"Se ha enviado la solicitud de revancha. Esperando al adversario", "Espere",
					JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Ups, No se pudo pedir revancha:\n" + e.getMessage(),
					"Error al marcar", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}

	private void abandonar() {
		try {
			controlador.abandonar();
			fin = true;
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Ups, No se pudo avisar el abandono de partida al servidor:\n" + e.getMessage(),
					"Error al marcar", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
}
